package ma.cdm.rapports;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import flex.messaging.FlexContext;

import ma.cdm.rapports.RuntimeExec.ExecutionResult;

public class ConvertorPDF2SWF   {

	//private static final Logger logger = Logger.getLogger(ConvertorPDF2SWF.class);
	
	/** Check and transform command */
	//private RuntimeExec checkCommand;
	private RuntimeExec transformCommand;

	/** Default flash version to convert to */
	private static final String DEFAULT_FLASH_VERSION = "10";

	/** Check command string */
	//private static final String PDF2SWF_CHECK = "tools\\pdf2swf -V";

	/** Transformation command string */
	private static final  String PDF2SWF_COMMAND = " ${source}   -t ${flashVersion}  -o ${target}";
	private static final String VAR_SOURCE = "source";
	private static final String VAR_TARGET = "target";
	private static final String VAR_FLASH_VERSION = "flashVersion";

	
	/**
	 * Helper to create a runtime exec object for a given command string
	 * 
	 * @param commandString     command string
	 * @return RuntimeExec      runtime exec command
	 */
	private RuntimeExec createCommand(String commandString)
	{
		// Create command
		RuntimeExec result = new RuntimeExec();

		// Set the command string
		Map<String, String> commandsByOS = new HashMap<String, String>(1);
		commandsByOS.put(".*", commandString);
		result.setCommandMap(commandsByOS);

		// Set the error code
		result.setErrorCodes("1");

		return result;
	}


//	/**
//	 * Get the check command for the PDF2SWF tool
//	 * 
//	 * @return  RuntimeExec check command
//	 */
//	private RuntimeExec getCheckCommand()
//	{
//		if (this.checkCommand == null)
//		{
//			this.checkCommand = createCommand(PDF2SWF_CHECK);
//		}
//
//		return this.checkCommand;
//	}

	/**
	 * Get the transform command for the PDF2SWF tool
	 * 
	 * @return  RuntimeExec transform command
	 */
	private RuntimeExec getTransformCommand()
	{
		if (this.transformCommand == null)
		{
			String pdfToSwfProgram = "pdf2swf_win";
			if(System.getProperty("os.name").toLowerCase().indexOf("win") >= 0)
			{
				pdfToSwfProgram = "pdf2swf_win";
			}
			else
			{
				pdfToSwfProgram = "pdf2swf_os";
			}
			//"C:\\Users\\cdm-u6688\\workspace\\GesCompteRendu\\src\\main\\webapp\\tools\\pdf2swf_win"+" "+PDF2SWF_COMMAND;
			String command =FlexContext.getServletContext().getRealPath("tools\\"+pdfToSwfProgram)+" "+PDF2SWF_COMMAND;
			this.transformCommand = createCommand(command);
		}
		return this.transformCommand;
	}


	
	public void convert(String inputFileName, String outputFileName) throws Exception
	{
		// create required temp files
		File sourceFile = new File(inputFileName);
		File targetFile = new File(outputFileName);
		String flashVersion = DEFAULT_FLASH_VERSION;
		Map<String, String> properties = new HashMap<String, String>(5);  
		properties.put(VAR_FLASH_VERSION, "-T " + flashVersion);
		properties.put(VAR_SOURCE, "\""+sourceFile.getAbsolutePath()+"\"");
		properties.put(VAR_TARGET, "\""+targetFile.getAbsolutePath()+"\"");
		
		// execute the transformation command
		ExecutionResult result = null;
		try
		{
			result = getTransformCommand().execute(properties);
		}
		catch (Throwable e)
		{
			throw new Exception("[ConvertorSWF] Transformation failed during command execution: \n" + transformCommand, e);
		}

		// check
		if (!result.getSuccess())
		{
			throw new Exception("[ConvertorSWF]  Transformation failed - status indicates an error: \n" + result);
		}

		// check that the file was created
		if (!targetFile.exists())
		{
			throw new Exception("[ConvertorSWF]  Transformation failed - target file doesn't exist: \n" + result);
		}       
	}
	
}
	

